package newtank.data;

import lombok.Data;
import server.GpcRequest;
import server.netty.GpcDataPackage;

import java.io.Serializable;

/**
 * 数据包
 * @author lijiahao
 * @since 2022/1/27
 */
@Data
public abstract class AbstractDataPackage implements Serializable, ToServerDataPackage, ToClientDataPackage {


    private static final long serialVersionUID = -3823425740029045678L;
    /**
     * 数据包序号
     */
    protected long index;
    /**
     * 状态位
     */
    protected int status;

    /**
     * 指令代码
     */
    protected int code;

    /**
     * 消息长度
     */
    protected int length;

    /**
     * 额外消息
     */
    protected String context;

    /**
     * 连接端编号
     */
    protected String unique;

    /**
     * 请求的context体
     */
    protected GpcRequest routeRequest;

    @Override
    public GpcDataPackage toData() {
        GpcDataPackage gpcDataPackage = new GpcDataPackage();
        gpcDataPackage.setUnique(this.unique);
        gpcDataPackage.setCode(this.code);
        gpcDataPackage.setIndex(this.index);
        gpcDataPackage.setStatus(this.status);
        gpcDataPackage.setLength(this.length);
        GpcRequest gpcRequest = new GpcRequest();
        gpcDataPackage.setRouteRequest(gpcRequest);
        filledData(gpcDataPackage);
        return gpcDataPackage;
    }

    protected abstract void filledData(GpcDataPackage gpcDataPackage);

    protected void filledDataHeader(GpcDataPackage gpcDataPackage, String header, String value) {
        if(gpcDataPackage == null) {
            return ;
        }
        if(gpcDataPackage.getRouteRequest() == null) {
            gpcDataPackage.setRouteRequest(new GpcRequest());
        }
        gpcDataPackage.getRouteRequest().setHeader(header, value);
    }

}
